<?php
// +----------------------------------------------------------------------
// | CoreThink [ Simple Efficient Excellent ]
// +----------------------------------------------------------------------
// | Copyright (c) 2014 http://www.corethink.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: jry <598821125@qq.com> <http://www.corethink.cn>
// +----------------------------------------------------------------------
namespace Admin\Controller;
use Think\Controller;


class BanksController extends AdminController
{
	public function index()
	{
		$map['bankFlag']=array('egt',0);
		
		$list=D("Banks")->page(!empty($_GET['p'])?$_GET['p']:1,C("ADMIN_PAGE_ROWS"))->where($map)->order("id asc")->select();
		
		$page=new \Common\Util\Page(D("Banks")->count(),C("ADMIN_PAGE_ROWS"));
		//右侧按钮
			$attr['field'] = 'bankFlag';
			$attr[0]['title'] = '启用';
			$attr[0]['class'] = 'label label-xq ajax-get confirm';
			$attr[0]['href'] = U('Admin/Banks/forbid', array(
					'id' => '__data_id__'
			));
			$attr['field'] = 'bankFlag';
			$attr[1]['title'] = '禁用';
			$attr[1]['class'] = 'label label-warning ajax-get confirm';
			$attr[1]['href'] = U('Admin/Banks/forbid', array(
					'id' => '__data_id__'
			));
	
			
		$builder = new \Common\Builder\ListBuilder();
		$builder->setMetaTitle("银行列表") //设置页面标题
		->addTopButton('addnew') //添加新增按钮
		->addTopButton('delete')
		->addTableColumn('id', 'ID')
		->addTableColumn('bankName', '银行名称')
		->addTableColumn('bankFlag', '状态','status')
		->addTableColumn('right_button', '操作', 'btn')
		->setTableDataList($list) //数据列表
		->setTableDataPage($page->show())  //数据列表分页
		->addRightButton('edit')   //添加编辑按钮
		->addRightButton('caseButton', '', $attr)
		->display();
	}
	public function forbid($id)
	{
		if($id)
		{
			$list=M("banks")->find($id);
			if(!empty($list))
			{
				if($list['bankFlag']==1)
				{
					$res=M('banks')->where("id=$id")->setField('bankFlag',0);
				
					if($res)
					{
						$this->success("禁用成功",U("index"));
				
					}else{
				
						$this->error("禁用失败",U("index"));
					}
				
				}else{
					$res=M('banks')->where("id=$id")->setField('bankFlag',1);
				
					if($res)
					{
						$this->success("启用成功",U("index"));
				
					}else{
						$this->error("启用失败",U("index"));
					}
				}
			}else{
				$this->error("没有符合条件的信息");
			}
		}
	}
	public function add()
	{
		if(IS_POST)
		{
			$bank_object = D('Banks');
			$data = $bank_object->create($_POST);
			if($data){
				$id = $bank_object->add();
					
				if($id){
					$this->success('新增成功', U('index'));
				}else{
					$this->error('新增失败');
				}
			}else{
				$this->error($bank_object->getError());
			}
			
		}else{
			
			$builder = new \Common\Builder\FormBuilder();
			$builder->setMetaTitle('新增')  //设置页面标题
			->setPostUrl(U('add')) //设置表单提交地址
			->addFormItem('bankName', 'text', '银行名称', '')
			->display();
		}
	}
	public function edit()
	{
		$id=I("id");
		if(IS_POST)
		{
			$data=$_POST;
			$data['status']=1;
			$data['bankFlag']=1;
			$bank_object = D('Banks');
			$data = $bank_object->create($data);
			if($data){
				$id = $bank_object->save();
					
				if($id){
					$this->success('修改成功', U('index'));
				}else{
					$this->error('修改失败');
				}
			}else{
				$this->error($bank_object->getError());
			}
				
		}else{
				if($id)
				{
					$list=D("Banks")->find($id);
					$builder = new \Common\Builder\FormBuilder();
					$builder->setMetaTitle('编辑')  //设置页面标题
					->setPostUrl(U('edit')) //设置表单提交地址
					->addFormItem('id', 'hidden', 'ID', '',$id)
					->addFormItem('bankName', 'text', '银行名称', '')
					->setFormData($list)
					->display();
				}
		}
	}

}